package com.tongzhou.client;

import java.util.List;

import net.sf.json.JSONObject;

import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.sales.dao.ISalesDAO;
import com.tongzhou.tongzhou.sales.dao.impl.SalesDAO;
import com.tongzhou.tongzhou.sales.model.Sales;
import com.tongzhou.tongzhou.sales.model.SearchSalesCondition;
import com.tongzhou.util.Util;


public class ClientPromotion {
	ISalesDAO dao=new SalesDAO();
	private SearchSalesCondition condition=null;
	private int pageSize ;
	private int currentPage ;
	private String orderBy ;
	private String orderByType ;
	public String getInfo(String json){
//		图url、标题、内容、电话号码

		JSONObject jsonObject = JSONObject.fromObject( json );
		Integer newNo=null;
		try{
			if(jsonObject.get("newNo")!=null){
				newNo=(Integer) jsonObject.get("newNo");
			}
			
			if(jsonObject.get("page")!=null){
				currentPage=(Integer) jsonObject.get("page");
			}
			
			if(jsonObject.get("pageNum")!=null){
				pageSize=(Integer) jsonObject.get("pageNum");
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		StringBuffer str=new StringBuffer();
		if(newNo==null){
			newNo=0;
		}
		try {
			checkSearchCondition();
			condition = new SearchSalesCondition();
			if (currentPage == 0){
				currentPage = 1;
			}
			condition.setCurrentPage(currentPage) ;
			condition.setOrderBy(orderBy);
			condition.setOrderByType(orderByType) ;
			condition.setPageSize(pageSize);
			condition.setType("1");
			
			condition.setNewNo(newNo);
			List<Sales> salesList=dao.getSalesListByPage(condition);
			if(salesList.size()>0){
				str.append("{'success':1,'msg':[");
				for(int i=0;i<salesList.size();i++){
					Sales n=salesList.get(i);
					str.append("{'no':'"+n.getId()+"',");
					str.append("'imageURL':'"+Util.changeToObj(n.getImage_url())+"',");
					str.append("'title':'"+Util.changeToObj(n.getTitle())+"',");
					str.append("'content':'"+Util.changeToObj(n.getContent())+"',");
					str.append("'quotCall':"+Util.changeToObj(n.getQuotCall())+"}");
					if(i<salesList.size()-1){
						str.append(",");
					}
				}
				str.append("]}");
			}else{
				str.append("{'success':0,'msg':'没有对应的促销信息!'}");
			}
			return str.toString();
		} catch (DBException e) {
			e.printStackTrace();
		}
		return "{'success':0,'msg':'输入参数有误!'}";
		
//		String str="{'success':1,'msg':[{'imageURL':'www.baidu.com','title':'标题','content':'内容','quotCall':'18938037662'},";
//		str+="{'imageURL':'www.baidu.com2','title':'标题2','content':'内容2','header':'18938037662'}]}";
//		return str;
	}
	/**
	 * 设置列表显示格式
	 *@author: 彭定雄
	 */
	private void checkSearchCondition(){
		if(currentPage <=0){
			currentPage = 1 ;
		}
		
		if(pageSize<=0){
			pageSize = 10 ;
		}
		
		if(orderBy==null||"".equals(orderBy)){
			orderBy = "id" ;
		}
		
		if(orderByType == null || "".equals(orderByType)){
			orderByType = "asc" ;
		}
	}
}
